package com.cmw.socket.core;

import java.io.IOException;

/**
 *
 * @ClassName IoContext
 * @Description: IO上下文类
 * @Author chengmingwei
 * @Date 2020/6/23  20:32
 * @Version V1.0
 **/
public class IoContext {
    //静态实例
    private static IoContext INSTANCE;
    private final IoProvider ioProvider;

    private IoContext(IoProvider ioProvider){
        this.ioProvider = ioProvider;
    }

    public IoProvider getIoProvider() {
        return ioProvider;
    }

    public static IoContext get(){
        return INSTANCE;
    }

    public static StartedBoot setup(){
        return new StartedBoot();
    }

    public static void  close() throws IOException {
        if(null != INSTANCE){
            INSTANCE.callClose();
        }
    }

    private void callClose() throws IOException {
        ioProvider.close();
    }

    /**
     *  静态 StartedBoot
     */
    public static class StartedBoot{
        private IoProvider ioProvider;

        private StartedBoot(){

        }

        public StartedBoot ioProvider(IoProvider ioProvider){
            this.ioProvider = ioProvider;
            return this;
        }

        public IoContext start(){
            INSTANCE = new IoContext(ioProvider);
            return INSTANCE;
        }
    }
}
